package com.example.industry.dao.impl;

import com.example.industry.MySqlConnector;
import com.example.industry.dao.UserDao;
import com.example.industry.entity.User;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@Repository
public class UserDaoImpl implements UserDao
{
    @Override
    public User findById(Integer id)
    {
        User user = new User();
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "select * from user where user_id = " + id;
            try (Statement stmt = conn.createStatement())
            {
                ResultSet resultSet = stmt.executeQuery(sql);
                if (resultSet.next())
                {
                    user.setUserId(resultSet.getInt("user_id"));
                    user.setUsername(resultSet.getString("username"));
                    user.setEmail(resultSet.getString("email"));
                    user.setPassword(resultSet.getString("password"));
                }
                else user = null;
            }
            connector.closeConnection();
            return user;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean addUser(User user) {
        if (user == null || user.getUsername() == null || user.getPassword() == null
        || user.getEmail() == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "insert into user ( user_name,user_email,user_password ) values( " + user.getUsername() + "," + user.getEmail() + "," + user.getPassword() + ")";
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result > 0) return Boolean.TRUE;
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean deleteById(Integer id) {
        User user = findById(id);
        if (user == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "delete from user where user_id = " + id;
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result > 0) return Boolean.TRUE;
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    @Override
    public Boolean update(User user) {
        if (user == null || user.getUsername() == null || user.getPassword() == null
                || user.getEmail() == null) return Boolean.FALSE;
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "update user set user_name = " + user.getUsername() +
                    ",user_email = " + user.getEmail() +
                    ",user_password = " + user.getPassword() +
                    " where user_id = " + user.getUserId();
            try (Statement stmt = conn.createStatement())
            {
                int result = stmt.executeUpdate(sql);
                if (result > 0) return Boolean.TRUE;
            }
            connector.closeConnection();
            return Boolean.FALSE;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    @Override
    public User findByEmail(String email) {
        User user = new User();
        MySqlConnector connector = new MySqlConnector();
        try
        {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "select * from user where user_email = " + email;
            try (Statement stmt = conn.createStatement())
            {
                ResultSet resultSet = stmt.executeQuery(sql);
                if (resultSet.next())
                {
                    user.setUserId(resultSet.getInt("user_id"));
                    user.setUsername(resultSet.getString("username"));
                    user.setEmail(resultSet.getString("email"));
                    user.setPassword(resultSet.getString("password"));
                }
                else user = null;
            }
            connector.closeConnection();
            return user;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public ResultSet findProjectsByUserId(Integer userId) {
        MySqlConnector connector = new MySqlConnector();
        try {
            connector.connectDatabase();
            Connection conn = connector.getConnection();
            String sql = "select * from user_project where user_id = " + userId;
            try (Statement stmt = conn.createStatement()) {
                ResultSet resultSet = stmt.executeQuery(sql);
                connector.closeConnection();
                return resultSet;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
